import axios from 'axios';
import { useToastStore } from '@/stores';
import router from '@/router';
import { useI18nStore, useUserInfoStore } from '@/stores';

// 创建 axios 实例
const http = axios.create({
  baseURL: '/app-api'
});

http.defaults.headers.put['Content-Type'] = 'application/json';

// 添加请求拦截器
http.interceptors.request.use(
  function (config) {
    const userInfo = JSON.parse(localStorage.getItem('userInfoStore'));
    // 在发送请求之前做些什么
    config.headers['Authorization'] =
      'Bearer ' + (userInfo?.okCloudDiskToken || '');

    // 检查请求 URL 是否包含上传头像的接口路径
    if (config.url.includes('/member/user/update-avatar')) {
      // 单独设置上传头像的请求的 Content-Type
      config.headers['Content-Type'] = 'multipart/form-data';
    } else {
      // 其他请求使用默认的 Content-Type
      config.headers['Content-Type'] = 'application/json';
    }

    return config;
  },
  function (error) {
    // 对请求错误做些什么
    return Promise.reject(error);
  }
);

// 添加响应拦截器
http.interceptors.response.use(
  function (response) {
    const toastStore = useToastStore();
    const i18nstore = useI18nStore();
    // 2xx 范围内的状态码都会触发该函数。
    // 对响应数据做点什么
    if (response.data.code === 0 || response.data.code === 1) {
      return response.data;
    }
    if (response.data.code === 512) {
      return response.data;
    }
    if (
      response.data.code === 400 &&
      response.data.msg === 'memberId不能为空'
    ) {
      toastStore.showToast({
        severity: 'error',
        detail: i18nstore.translate('login_status_expired'),
        life: 3000,
        group: 'upload_failed'
      });
      localStorage.removeItem('userInfoStore');
      setTimeout(() => {
        router.push('/login');
      }, 3000);
      return Promise.reject(response.data);
    }
    if (response.data.code === 401) {
      const { userInfo, okCloudDiskToken, tokenTimeStamp } = storeToRefs(
        useUserInfoStore()
      );
      toastStore.showToast({
        severity: 'error',
        detail: i18nstore.translate('login_status_expired'),
        life: 3000,
        group: 'upload_failed'
      });
      localStorage.removeItem('userInfoStore');
      userInfo.value = null;
      okCloudDiskToken.value = null;
      tokenTimeStamp.value = null;
      router.push('/login');
      setTimeout(() => {
        router.push('/login');
      }, 3000);
      return Promise.reject(response.data);
    }
    if (response.data.code === 403) {
      toastStore.showToast({
        severity: 'error',
        detail: i18nstore.translate('no_permission'),
        life: 3000,
        group: 'upload_failed'
      });
      return Promise.reject(response.data);
    }

    toastStore.showToast({
      severity: 'error',
      detail: response.data.msg,
      life: 3000,
      group: 'upload_failed'
    });
    return Promise.reject(response.data);
  },
  function (error) {
    // if (error.response.status === 500) {
    //   const toastStore = useToastStore();
    //   const i18nstore = useI18nStore();
    //   toastStore.showToast({
    //     severity: 'error',
    //     detail: i18nstore.translate('network_error'),
    //     life: 3000,
    //     group: 'upload_failed'
    //   });
    //   localStorage.removeItem('userInfoStore');
    //   return Promise.reject(error);
    // }
    // 对响应错误做点什么
    return Promise.reject(error);
  }
);

export default http;
